﻿#include "ScaleCommand.h"

#include <QDebug>

ScaleCommand::ScaleCommand(GraphicsBaseItem* item,
	const GraphicsItemInfo& oldItemInfo,
	const QMap<GraphicsBaseItem*, GraphicsItemInfo>& oldChildPosMap,
	const GraphicsItemInfo& newItemInfo,
	const QMap<GraphicsBaseItem*, GraphicsItemInfo>& newChildPosMap)
	: BaseCommand()
	, m_item(item)
	, m_oldItemInfo(oldItemInfo)
	, m_oldChildInfoMap(oldChildPosMap)
	, m_newItemInfo(newItemInfo)
	, m_newChildInfoMap(newChildPosMap)
{
}

ScaleCommand::~ScaleCommand()
{

}

void ScaleCommand::undo()
{
	m_item->getItemData().setItemScale(m_oldItemInfo.scale);
	m_item->setPos(m_oldItemInfo.pos);
	m_item->getItemData().setScaleSize(m_oldItemInfo.rect.size());
	m_item->refresh();
	if (GraphicsItemType::Box_Enum == m_item->getItemData().getBaseItemType()) {
		GraphicsBoxItem* boxItem = dynamic_cast<GraphicsBoxItem*>(m_item);
		boxItem->fitToScreen();
	}
	for (GraphicsBaseItem* item : m_oldChildInfoMap.keys()) {
		const QPointF oldPos = m_oldChildInfoMap[item].pos;
		item->setPos(oldPos);
	}
}

void ScaleCommand::redo()
{
	if (m_isFirstPush) {
		m_isFirstPush = false;
		return;
	}
	m_item->getItemData().setItemScale(m_newItemInfo.scale);
	m_item->setPos(m_newItemInfo.pos);
	m_item->getItemData().setScaleSize(m_newItemInfo.rect.size());
	m_item->refresh();
	if (GraphicsItemType::Box_Enum == m_item->getItemData().getBaseItemType()) {
		GraphicsBoxItem* boxItem = dynamic_cast<GraphicsBoxItem*>(m_item);
		boxItem->fitToScreen();
	}
	for (GraphicsBaseItem* item : m_newChildInfoMap.keys()) {
		const QPointF newPos = m_newChildInfoMap[item].pos;
		item->setPos(newPos);
	}
}
